package team.dmh.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team.dmh.wx.cache.QRCodeStatus;
import team.dmh.wx.cache.RedisService;
import team.dmh.wx.entity.User;
import team.dmh.wx.enums.QRCodeTypeEnum;
import team.dmh.wx.service.IUserService;
import team.dmh.wx.util.IDUtils;
import team.dmh.wx.util.Result;
import team.dmh.wx.util.ResultUtils;
import team.dmh.wx.vo.UserVO;

/**
 * @Auther: Hz1kai
 * @Date: 2018/10/22 15:06
 * @Description:
 */

@Api(tags = "二维码操作")
@RestController
@RequestMapping("/qrcode")
@Slf4j
public class QRCodeController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private IUserService userService;

    @ApiOperation("获取唯一id进行页面二维码生成")
    @GetMapping("/getId")
    public Result getQRCodeId() {
        Result result = new Result();
        //生成唯一的二维码id返回页面生成二维码，过期时间为5分钟
        String qrCodeId = IDUtils.getInstance().nextId();
        redisService.set(qrCodeId, new QRCodeStatus(QRCodeTypeEnum.NOT_EXPIRED), 1800);
        result.put("qrCodeId", qrCodeId);
        return result.success();
    }

    @ApiOperation("获取二维码id当前状态")
    @GetMapping("/getStatus")
    public Result getQRCodeStatus(String qrCodeId) {
        Result result=new Result();
        int expireTime = (int) redisService.getExpire(qrCodeId);
        if (expireTime == -1) {
            result.fail().info(QRCodeTypeEnum.EXPIRED.msg).put("code",QRCodeTypeEnum.EXPIRED.type);
            return result;
        }
        //判断二维码是否存在
        boolean isExist = redisService.hasKey(qrCodeId);
        if (isExist) {
            QRCodeStatus qrCodeStatus = (QRCodeStatus) redisService.get(qrCodeId);
            //如果为已经登录成功但未作废，则返回二维码已被使用
            if(qrCodeStatus.getType()==QRCodeTypeEnum.LOGIN_SUCCESS.type){
                result.fail().info(QRCodeTypeEnum.USE_ALREADY.msg).put("code",QRCodeTypeEnum.USE_ALREADY.type);
            }else if (qrCodeStatus.getType()==QRCodeTypeEnum.NOT_EXPIRED.type){
                result.success().info(QRCodeTypeEnum.NOT_EXPIRED.msg).put("code",qrCodeStatus.getType());
            }
            return result;
        } else {
            result.fail().info(QRCodeTypeEnum.NOT_EXIST.msg).put("code",QRCodeTypeEnum.NOT_EXIST.type);
            return result;
        }
    }

    @ApiOperation("登陆过后对二维码进行过期处理")
    @GetMapping("/expireQRCode")
    public Result expireQRCode(String qrCodeId) {
        int expireTime = (int) redisService.getExpire(qrCodeId);
        if (expireTime == -1) {
            return ResultUtils.fail(QRCodeTypeEnum.EXPIRED.msg);
        }
        boolean isExist = redisService.hasKey(qrCodeId);
        if (isExist) {
            QRCodeStatus expireCode = new QRCodeStatus(QRCodeTypeEnum.USE_ALREADY);
            redisService.set(qrCodeId, expireCode);
            return ResultUtils.success();
        } else {
            return ResultUtils.fail(QRCodeTypeEnum.NOT_EXIST.msg);
        }
    }

    @ApiOperation("根据扫描获取的id进行登录")
    @GetMapping("/confirmLogin")
    public Result confirmLogin(String qrCodeId, String userId) {

        int expireTime = (int) redisService.getExpire(qrCodeId);
        if (expireTime == -1) {
            return ResultUtils.fail(QRCodeTypeEnum.EXPIRED.msg);
        }
        //修改二维码状态为登录状态,放入userId
        boolean isExist = redisService.hasKey(qrCodeId);
        if (isExist) {
            QRCodeStatus qrCodeStatus = (QRCodeStatus) redisService.get(qrCodeId);
            Integer type = qrCodeStatus.getType();
            Result result=new Result();
            if (type == QRCodeTypeEnum.NOT_EXPIRED.type) {
                User user = userService.getById(userId);
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(user, userVO);
                qrCodeStatus.setUser(userVO).setType(QRCodeTypeEnum.LOGIN_SUCCESS.type).setMsg(QRCodeTypeEnum.LOGIN_SUCCESS.msg);
                redisService.set(qrCodeId, qrCodeStatus);
                result.success().info(QRCodeTypeEnum.LOGIN_SUCCESS.msg)
                        .put("code",QRCodeTypeEnum.LOGIN_SUCCESS.type);
                result.put("user",userVO);
                return result;
            } else if (type == QRCodeTypeEnum.LOGIN_SUCCESS.type || type == QRCodeTypeEnum.USE_ALREADY.type) {
                result.fail().info(QRCodeTypeEnum.USE_ALREADY.msg).put("code",QRCodeTypeEnum.USE_ALREADY.type);
                return result;
            }
        }
        return ResultUtils.fail(QRCodeTypeEnum.NOT_EXIST.msg);

    }
}
